package com.yklx.youke.admin.mapper;

import com.yklx.youke.entity.Permission;
import org.apache.ibatis.annotations.*;

import java.util.List;

@org.apache.ibatis.annotations.Mapper
public interface PermissionMapper {
    /**
     * 根据用户ID获取权限列表
     * @param userId 用户ID
     * @return 权限列表
     */
    @Select("SELECT p.* FROM permissions p " +
            "JOIN role_permissions rp ON p.permission_id = rp.permission_id " +
            "JOIN user_roles ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId}")
    List<Permission> getUserPermissions(@Param("userId") String userId);

    /**
     * 获取所有权限列表
     * @return 所有权限列表
     */
    @Select("SELECT * FROM permissions ORDER BY permission_name")
    List<Permission> getAllPermissions();

    /**
     * 根据权限ID获取权限
     * @param permissionId 权限ID
     * @return 权限对象
     */
    @Select("SELECT * FROM permissions WHERE permission_id = #{permissionId}")
    Permission getPermissionById(@Param("permissionId") String permissionId);

    /**
     * 根据权限代码获取权限
     * @param permissionCode 权限代码
     * @return 权限对象
     */
    @Select("SELECT * FROM permissions WHERE permission_code = #{permissionCode}")
    Permission getPermissionByCode(@Param("permissionCode") String permissionCode);

    /**
     * 插入新权限
     * @param permission 权限对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO permissions (permission_id, permission_name, permission_code, permission_description, created_at, updated_at) " +
            "VALUES (#{permissionId}, #{permissionName}, #{permissionCode}, #{permissionDescription}, NOW(), NOW())")
    int insertPermission(Permission permission);

    /**
     * 更新权限
     * @param permission 权限对象
     * @return 影响的行数
     */
    @Update("UPDATE permissions SET permission_name=#{permissionName}, permission_code=#{permissionCode}, " +
            "permission_description=#{permissionDescription}, updated_at=NOW() WHERE permission_id=#{permissionId}")
    int updatePermission(Permission permission);

    /**
     * 删除权限
     * @param permissionId 权限ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM permissions WHERE permission_id = #{permissionId}")
    int deletePermission(@Param("permissionId") String permissionId);
}